Note: There are often multiple ways to answer each question.

  1. Assign the character string “11” to x and the character string “22” to y.
x <- "11"
y <- "22"
  1. What is the result of x + y? Why does R return this result?
x + y
## Error in x + y: non-numeric argument to binary operator

Both x and y are character variables, and R does not have an in-built way of using the + operator to add character variables together.

  1. If we wanted to treat the contents of x and y as numbers and add them, how could we do that?
as.numeric(x) + as.numeric(y)
## [1] 33
  1. Save the (numeric) result from Qn 3 in the variable z. Compute the square root of z.
z <- as.numeric(x) + as.numeric(y)
sqrt(z)  # soln 1
## [1] 5.744563
z^0.5    # soln 2
## [1] 5.744563
  1. Remove all the variables that you have created from the environment.
rm(list = ls())
  1. Your friend Jack wants to print the string “STATS_32” to the console. He enters the following command but encounters an error. What went wrong? What should he have done instead?
print(STATS_32)
## Error in print(STATS_32): object 'STATS_32' not found

R interprets STATS_32 as the name of a variable and since it does not exist, it complains, saying it did not find an object/variable named STATS_32. Jack should have put quotation marks around STATS_32 instead:

print("STATS_32")
## [1] "STATS_32"
  1. What is the result of NA + 2? Why do you think that is?
NA + 2
## [1] NA

We can think of NA as a placeholder for “I don’t know”. If we add 2 to “I don’t know”, we still don’t know the result!